
//#include <aos/aos.h>
//#include "caipu.h"

#ifndef SRC_XLINKAPP_LJ_PROTO_USART_H_
#define SRC_XLINKAPP_LJ_PROTO_USART_H_

#define lj_uchar unsigned char
#define  lj_uint unsigned int
#define lj_uint_16 unsigned short
#define lj_char char
#define lj_int int

//#define LOCAL_CONN_ENABLE//del

// #include <diag.h>

#define u_printf printf

#define data_out_max (512)
#define data_in_max 256
#define lj_point_max 1000
#define data_in_out_max 256
#define time_count_set 100//set pkt_init date

#define CONTROL_BUF_MAX (512)

#define LJ_VER 01

#define zzc
//#define zzc_v2
#define zzc_hf
#define zzc_zijian
#define zzc_fast
#define zzc_smart_send_status
#define zzc_smart_clock
//#define zzc_kind
//#define zzc_alarm
//#define zzc_game
//#define zzc_test
//#define zzc_qiangdu
//#include "hsf.h"

//#define pobiji2
#define yanji
#define peiwang
//#define cizhuan
//#define yanji_real
//#define pobiji1
//#define kaoxiang
//#define ganyiji
//#define kaoxiang_new

//#define tianjun
#define self

// #define _0319_//pobiji
 #define _make_out_use_ring_//not same time with _0319_

#define _up_make_out_use_ring_
#ifdef _up_make_out_use_ring_
extern void up_put_ring(unsigned char *buf,unsigned int len);
extern unsigned char up_send_ring(void);
#endif//_up_make_out_use_ring_



//#define _i2c_
//#define _chaoren_jhq_//
#define _04_reset_


//#define special
//#define _meizhi_
//#define bigualu
//#define chuang

//#define _single_error_

#define add_ff

//#define _use_caipu_
#define _use_send_comfirm_
#define REPEAT_TIME_MAX (2)
#define TIME_CAN_UP_MAX (10*60*10)//100ms

//#define _CAIPU_USE_STRING_


#define _zijian2_
#define _power_safe_

#define change  //�ı䴮�ڽ��պ���
#define queen
#define zzc_retry_less//ȡ����������

//#define each//ÿ��������һ����//qunuanqi


// #define use_https
#ifdef _0319_
#include "xlink_ringbuf.h"
extern Kfifo  ring_u_data;
#endif
//#define _hex_

#define LJ_KIND_CHECK 0X55

typedef struct {
	unsigned char kind;
	unsigned char model;
	unsigned char check;
}lj_kind_st;

extern lj_kind_st lj_kind;

#define CMD_MAX	  40
#define HEX_OFFSET 0
typedef struct{
	unsigned int len;
	unsigned char buf[CMD_MAX];
	unsigned char sign;
}hex_cmd_st;

typedef enum
{
	d_dir_in=0x01,
	d_dir_out=0x02,
}d_dir;

typedef enum
{
	c_get_mac=0x00,
	c_get_connect=0x01,
	c_get_idkey=0x02,
	c_set_idkey=0x03,
	c_smart_link=0x04,
	c_restart=0x05,
	c_reset=0x06,
	c_get_ver=0x07,
	c_get_time=0x08,
	c_set_dis=0x09,
	c_get_idtoken=0x0a,
	c_set_idtoken=0x0b,
	c_unbind=0x0c,
	c_unfind=0x0d,
	c_set_hex=0x0e,
	c_set_kind=0x0f,
	c_get_kind=0x10,
	c_zijian=0x11,
	c_qiangdu=0x12,
	c_reload=0x13,
	c_get_ssid_pwd=0x14,
	c_set_ssid_pwd=0x15,

	c_send_data=0x80,
	c_rec_data=0x81,
	c_send_point=0x82,
	c_rec_point_push=0x83,
	c_rec_point_all=0x84,
	c_rec_point_npush=0x85,
	c_rec_hex_npush=0x86,
	c_send_hex=0x87,

	c_get_game_id=0x90,

	c_test_get_id_work=0x20,
	c_test_reset=0x21,
	c_test_set_debug=0x22,
	c_get_ver_lj=0x23,
	c_scan_ap=0x24,
	c_send_wifi_name=0x25,
	c_ask_wifi=0x26,
	c_set_wifi=0x27,
	c_at=0x28,
	c_set_wifi_name_key=0x29,
	c_set_baud=0x2a,
	c_set_use_spp=0x2b,
	c_get_net_kind=0x2c,
	c_send_sta_ap=0x2d,
	c_test=0x2f,
	c_set=0x30,



	m_control=0x80,
		m_reset=0xe0,
		m_net=0xe1,
		m_query=0xe3,
		m_status=0x90,

//	c_test_get_id_work=0x20,
//	c_test_reset=0x21,
//	c_test_set_debug=0x22,
//	c_get_ver_lj=0x23,


}c_cmd;

typedef enum{
	ty_hb=0x00,
	ty_ask=0x01,
	ty_net=0x03,
	ty_reset=0x04,
	ty_reset_mode=0x05,
	ty_control=0x06,
	ty_notify=0x07,
	ty_notify_need_ack=0x22,
	ty_notify_ack=0x23,
	ty_query=0x08,
	ty_get_time=0x1c,
	ty_test_wifi=0x0e,
	ty_rssi=0x24,
	ty_ask_net=0x2b,

}ty_cmd;



typedef enum
{
	LJ_DP_BYTE  = 0x00,
	LJ_DP_INT16 = 0x10,
	LJ_DP_UINT16 = 0x20,
	LJ_DP_INT32 = 0x30,
	LJ_DP_UINT32 = 0x40,
	LJ_DP_INT64 = 0x50,
	LJ_DP_UINT64 = 0x60,
	LJ_DP_FLOAT = 0x70,
	LJ_DP_DOUBLE = 0x80,
	LJ_DP_STRING = 0x90,
	LJ_DP_BINARY = 0XA0
}LJ_DATAPOINTTYPE;



typedef struct
{
	lj_uchar cmd;
	lj_uchar type;
	lj_uchar id_dir;
	lj_uchar check_num;
	lj_uchar check_num1;
	lj_uchar len_h;
	lj_uchar len_l;
	lj_uint_16 len_num;
	lj_uchar r_id;
	lj_uchar s_id;
	lj_uchar* data;
	lj_uchar time_out;
	lj_uchar step;
	lj_uchar ff_step;
	lj_uint_16 count;
	lj_uchar check_num_buf;
	lj_uint time_count;
	unsigned char repeat_times;
	unsigned char ver;

}lj_usart_pkt;


typedef struct
{
	lj_uint_16 year;
	lj_uchar month;
	lj_uchar day;
	lj_uchar hour;
	lj_uchar minute;
	lj_uchar second;
	lj_uchar area;
	lj_uchar week;
}lj_time_t;

//alarm
#define ALARM_OFFSET 50
#define ALARM_MAX 10////the max number of alarm
#define ALARM_LEN 8//the len of each alarm buf
#define ALARM_LEN_4 4//no month day year
#define ALARM_LEN_6 6//no year
#define ALARM_LEN_8 8//wiht all
#define ALARM_LEN_SET  ALARM_LEN_4
#define ALARM_KIND 0xA0//uint //alarm point kind
#define ALARM_NUM 2//the used number of alarm
//#define ALARM_OUT_POINT 0X12//the point index of alarm send out when fit the time
#define TIME_YEAR_H 0X07//2018
#define TIME_YEAR_L 0xe2
#define ALARM_POINT 0x12//the point index of alarm send out when fit the time
#define ALARM_GET_POINT 0X13//query
extern const unsigned char alarm_cmd[ALARM_MAX];
extern const unsigned char alarm_onoff_cmd[ALARM_MAX];

extern unsigned char all_wifi;
extern unsigned char all_net;
extern unsigned char all_error;


typedef struct{
	unsigned char onoff;
	unsigned char buf[ALARM_LEN];
}alarm_st;

typedef struct{
	alarm_st alarm[ALARM_MAX];
}alarm_all_st;


//game

#define GAME_OFFSET 200
#define KIND_OFFSET 210


//typedef  void(*lj_usart_pkt_cb)(*lj_usart_pkt);
typedef void(*lj_usart_pkt_send_cb)(lj_char *,lj_uint_16);
typedef void(*lj_reset_sdk_cb)(void);
typedef void(*lj_smart_link_cb)(void);
typedef lj_uchar(*lj_ask_net_cb)(lj_uchar*,lj_uchar*);
typedef lj_uchar(*lj_restart_cb)(void);
typedef lj_uchar(*lj_reload_cb)(void);
typedef lj_uchar (*lj_get_mac_cb)(lj_uchar*);
typedef lj_uchar (*lj_get_idkey_cb)(lj_uchar*,unsigned int *);
typedef lj_uchar (*lj_set_idkey_cb)(lj_uchar*,lj_uint_16);
typedef void(*lj_delayms_cb)(lj_uint_16);
typedef void(*lj_get_time_cb)(lj_time_t*);
typedef void(*lj_get_ver_cb)(lj_uchar*,lj_uint_16*);
typedef lj_uchar (*lj_publish_cb)(lj_uchar *,lj_uint_16);
typedef lj_uchar (*lj_publish_point_push_cb)(lj_uchar *,lj_uint_16);
typedef lj_uchar (*lj_publish_point_npush_cb)(lj_uchar *,lj_uint_16);
typedef lj_uchar (*lj_publish_point_npush_raw_cb)(lj_uchar *,lj_uint_16,lj_usart_pkt);
typedef lj_uchar (*lj_publish_point_npush_raw_85_cb)(lj_uchar *,lj_uint_16);
typedef lj_uchar (*ljhf_get_ssid_pwd_cb)(char *buf);
typedef lj_uchar (*ljhf_set_ssid_pwd_cb)(char *buf);
typedef void (*ljhf_test_cb)(unsigned char *buf,unsigned int len);



typedef lj_uchar (*lj_init_point_all_cb)(lj_uchar *,lj_uint_16);
typedef void(*lj_unfind_cb)(void);
typedef void(*lj_unbind_cb)(void);
typedef void(*lj_set_dis_cb)(lj_uchar);
typedef lj_uint_16 (*lj_usend_cb)(lj_uchar *,lj_uint_16);
typedef lj_uchar (*lj_get_id_work_cb)(lj_uchar*,lj_uchar *);

typedef void(*lj_set_cb)(unsigned char *buf,unsigned char len);

typedef struct
{
//	lj_usart_pkt_send_cb usart_send;
	lj_reset_sdk_cb reset_sdk;
	lj_restart_cb restart;
	lj_reload_cb reload;
	lj_smart_link_cb smart_link;
	lj_ask_net_cb ask_net;
	lj_get_mac_cb get_mac;
	lj_get_idkey_cb get_idkey;
	lj_set_idkey_cb set_idkey;
	lj_delayms_cb delayms;
	lj_get_ver_cb get_ver;
	lj_get_time_cb get_time;
	lj_publish_cb publish;
	lj_publish_point_push_cb publish_point_push;
	lj_publish_point_npush_cb publish_point_npush;
	lj_publish_point_npush_raw_cb publish_point_npush_raw;
	lj_publish_point_npush_raw_85_cb publish_point_npush_raw_85;
	lj_init_point_all_cb init_point_all;
	lj_unfind_cb unfind;
	lj_unbind_cb unbind;
	lj_set_dis_cb set_dis;
	lj_usend_cb usend;
	lj_get_id_work_cb get_id_work;
	ljhf_get_ssid_pwd_cb get_ssid_pwd;
	ljhf_set_ssid_pwd_cb set_ssid_pwd;

//	lj_set_cb set;

	ljhf_test_cb test;


}lj_usart_pkt_st;

extern void make_out(lj_usart_pkt *pkt);


extern void lj_usart_pkt_st_init(lj_usart_pkt_st * pkt_st_buf);

extern void lj_usend_control(lj_uchar* data,lj_uint_16 data_len);
extern void lj_usend_control_raw(unsigned char *buf,unsigned int len);
extern void lj_usart_get_deal(lj_uchar* buf,lj_uint_16 len);
extern void lj_usend_wifi_net_status(lj_uchar wifi,lj_uchar net);



extern void time_usart_fun(void);

extern void alarm_work_loop(void);


extern void flash_write_kind(void);
extern void flash_read_kind(void);


extern void lj_usend_time_1(lj_time_t time_buf);
#ifdef zzc_zijian
extern void flash_write_zijian(void);
extern void flash_read_zijian(void);
extern void zijian_init(void);
extern void zijian_loop(void);
#endif



#ifdef _zijian2_
void lj_usend_zijian(unsigned char res);
#endif

extern unsigned char send_ring(unsigned char get_feedback);
extern unsigned char need_send_status;
extern void net_send_status(void);

extern void time_get_deal_loop(void);
extern void print_x(lj_uchar*data,lj_uint_16 len);
extern void notify_init(void);

#define LJ_TIME_CAN_PUSH_ORI  (10*60*240)//100ms

#ifdef _use_caipu_
 extern unsigned char lj_push_mode;//caipu
 extern unsigned char lj_setting[POINT_ALL_NUM_MAX+1];
 extern int lj_setting_len;
#endif //_use_caipu_
 extern unsigned int lj_time_can_push;
 extern unsigned char get_feedback;

 extern void air_send_zhufan_time_loop(void);//100ms

 extern unsigned int zhufan_time_min_total;


#define HAD_CHANGE_ALL (0X01)
#define HAD_CHANGE_SETTING (0x10)

 extern unsigned int time_can_up;
 extern unsigned int time_can_up_dingshi;
 extern unsigned int time_can_up_dingshi_set;

extern void lj_usend_zijian_ty(unsigned char res, signed char rssi);

#endif /* SRC_XLINKAPP_LJ_PROTO_USART_H_ */
